package cn.itcast.zookeeper.export;

import cn.itcast.zookeeper.product.ProductServiceImpl;
import cn.itcast.zookeeper.service.ProductService;
import org.I0Itec.zkclient.ZkClient;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class ServiceExport {
    /**
     * 服务注册中心根节点path
     *
     *   持久化节点：
     *   /dubbo/cn.itcast.service.HelloService
     *   /dubbo/cn.itcast.service.HelloService/providers
     *   临时节点： 存储每次发布服务的临时数据
     *    192.168.2.10:8080/接口名称?版本?接口服务名称?其他信息等
     *    /dubbo/cn.itcast.service.HelloService/providers/dubbo://192.168.3.12:20880/cn.itcast.service.HelloService?anyhost=true&application=provider-dubbo&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=cn.itcast.service.HelloService&methods=sayHello&pid=2108&release=2.7.5&revision=1.0.0&side=provider×tamp=1600477179822&version=1.0.0
     * */
    private static final String ROOT_NODE = "/heima_mall";

    /**
     * 对外提供服务的缓存信息
     * */
    private static Map<String,Object> exportServices = new HashMap<String, Object>();

    static {
        // 对外提供服务初始化
        exportServices.put(ProductService.class.getName(),new ProductServiceImpl());
    }

    public static void main(String[] args) {
        //  服务生产者  将商品服务暴露出去
        //创建zk连接  初始化服务节点
        ZkClient zkClient = new ZkClient("192.168.106.128:2181");
        System.out.println("-------初始化节点开始-------------");
        String  providersPath = initNodes(zkClient,exportServices);
        System.out.println(providersPath+" -------节点创建完成-----服务暴露OK--------   : ");
        System.out.println("输入任意内容进行关闭程序...");
        //等待关闭
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //zk连接关闭
        zkClient.close();
    }

    private static String initNodes(ZkClient zkClient,Map<String ,Object> exportServices) {
        String  providersPath=null;
      try{
          //初始化服务提供IP和端口号，我们这边通过控制台手动输入来模拟
          Scanner scanner = new Scanner(System.in);
          System.out.println("请输入对外提供的服务端口格式: ip:port");
          //控制台输入对外暴露的服务地址
          String serverAddress = null;
          if(scanner.hasNextLine()){
              serverAddress = scanner.nextLine();
          }
          if(serverAddress == null || "".equals(serverAddress.trim())){
              System.out.println("你没有输入有效的内容,请重启服务再次输入！");
              return  null;
          }

          if(zkClient.exists(ROOT_NODE)==false){
              zkClient.createPersistent(ROOT_NODE);
          }
          //遍历所有的接口，将接口数据注册到注册中心
          for(Map.Entry entry : exportServices.entrySet()){
              String serviceName = (String) entry.getKey();
              String servicePath = ROOT_NODE + "/" + serviceName;  //   /heimal_mall/cn.itcast.xxxService
              String providorPath = servicePath + "/providers";
              //创建service节点
              if(zkClient.exists(servicePath) == false){
                  zkClient.createPersistent(servicePath);
              }
              //创建providor节点
              if(zkClient.exists(providorPath) == false)
              {
                  zkClient.createPersistent(providorPath); //  /heimal_mall/cn.itcast.xxxService/providers
              }
                 providersPath = providorPath + "/dubbo%3A%2F%2F" + serverAddress + "%2Finterface%3Fdubbo%3D2.0.2%26interface%3D"+serviceName;
              //创建当前服务的具体地址临时节点
              zkClient.createEphemeral(providorPath + "/dubbo%3A%2F%2F" + serverAddress + "%2Finterface%3Fdubbo%3D2.0.2%26interface%3D"+serviceName,true);
          }
          return providersPath;
      }catch (Exception e){
            e.printStackTrace();
            throw new  RuntimeException(e.getMessage());
      }
    }
}
